#include "CountDownState.h"
#include "ScaleAnimation.h"
#include <sstream>

CountDownState::CountDownState(GameState* nextState)
    : nextState(nextState), text(NULL), time(0), count(3)
{
}

CountDownState::~CountDownState()
{
}

GraphicalElement* CountDownState::createGraphics()
{
    return text;
}

void CountDownState::init()
{
    text = new Text(Position(200, 100), "3", 200, Color(255, 255, 0));
    updateText();
}

void CountDownState::updateText()
{
    std::ostringstream oss;
    oss << count;
    text->setText(oss.str());
    text->setAnimation(new ScaleAnimation(2, 1000));
}

GameState* CountDownState::update(unsigned long msSinceLastLoop)
{
    time += msSinceLastLoop;
    if (time / 1000 > (3 - count))
    {
        count--;
        updateText();
    }

    if (count == 0)
    {
        return nextState;
    }
    return this;
}
